/*
 * name: mabel xu
 * date: february 25, 2008
 * filename: TreeSetTreeMap.java
 * description: implement TreeSet TreeMaps
 */

import java.util.*;	// TreeSet and TreeMap classes

public class TreeSetTreeMap
{
	public static void main(String[] args)
	{
		StudentReader in;
		try	// try to create new StudentReader
		{
			in = new StudentReader("students.txt");
		}
		catch (Exception e)	// oh nos, student input file cannot be created!
		{
			System.out.println("something wrong with student reader ... exiting");
			return;
		}		// end try ... catch to create StudentReader input

		TreeSet<StudentID> studentSetCompareTo = new TreeSet<StudentID>();
		TreeSet<StudentID> studentSetComparator = new TreeSet<StudentID>(new StudentComparator());
		TreeMap<Integer, StudentID> studentMap = new TreeMap<Integer, StudentID>();

		// add students
		ArrayList<StudentID> adds = in.getAdds("QUIT");
		for (StudentID s: adds)	// add each student to all three structures
		{
			studentSetCompareTo.add(s);
			studentSetComparator.add(s);
			studentMap.put(s.getID(), s);
		}		// end of loop to add students
		System.out.println();

		System.out.println("after adding ...");
		print(studentSetCompareTo);
		print(studentSetComparator);
		print(studentMap);
		System.out.println();

		// change students
		ArrayList<StudentID[]> changes = in.getChanges("QUIT");
		for (StudentID[] s: changes)	// try to change each student to be changed
		{
			if (studentSetCompareTo.contains(s[0]))		// if student found in first set
			{
				studentSetCompareTo.remove(s[0]);
				studentSetCompareTo.add(s[1]);
			}
			else	// if student not found in first set
			{
				System.out.println("Student not found to change (set - compare to): " + s[0]);
			}		// end of trying to change student in first set

			if (studentSetComparator.contains(s[0]))	// if student found in second set
			{
				studentSetComparator.remove(s[0]);
				studentSetComparator.add(s[1]);
			}
			else	// if student not found in second set
			{
				System.out.println("Student not found to change (set - comparator): " + s[0]);
			}		// end of trying to change student in second set

			if (studentMap.containsKey(s[0].getID()))	// if student found in map
			{
				studentMap.put(s[1].getID(), s[1]);
			}
			else	// if student not found in map
			{
				System.out.println("Student not found to change (map): " + s[0]);
			}		// end of trying to change student in map
		}		// end of loop to change each student
		System.out.println();

		System.out.println("after changing ...");
		print(studentSetCompareTo);
		print(studentSetComparator);
		print(studentMap);
		System.out.println();

		// remove students
		ArrayList<StudentID> removes = in.getRemoves("QUIT");
		for (StudentID s: removes)	// for each student to be removed ...
		{
			// try to remove student from first set
			if (studentSetCompareTo.contains(s))
				studentSetCompareTo.remove(s);
			else
				System.out.println("Student not found to remove (set - compare to): " + s);

			// try to remove student from second set
			if (studentSetComparator.contains(s))
				studentSetComparator.remove(s);
			else
				System.out.println("Student not found to remove (set - comparator): " + s);

			// try to remove student from map
			if (studentMap.containsKey(s.getID()))
				studentMap.remove(s.getID());
			else
				System.out.println("Student not found to remove (map): " + s);
		}			// end of loop to try removing each student
		System.out.println();

		System.out.println("after removing ...");
		print(studentSetCompareTo);
		print(studentSetComparator);
		print(studentMap);
		System.out.println();

		// find students
		ArrayList<StudentID> finds = in.getFinds("QUIT");
		for (StudentID s: finds)	// for each student to be found ...
		{
			// try to find student in first set
			if (studentSetCompareTo.contains(s))
				System.out.println("Student found (set - compare to): " + s);
			else
				System.out.println("Student not found (set - compare to): " + s);

			// try to find student in second set
			if (studentSetComparator.contains(s))
				System.out.println("Student found (set - comparator): " + s);
			else
				System.out.println("Student not found (set - comparator): " + s);

			// try to find student in map
			if (studentMap.containsKey(s.getID()))
				System.out.println("Student found (map): " + s);
			else
				System.out.println("Student not found (map): " + s);
		}			// end of trying to find each student to be found
		System.out.println();

		System.out.println("after finding ...");
		print(studentSetCompareTo);
		print(studentSetComparator);
		print(studentMap);
		System.out.println();
	}		// end of main method of TreeSetTreeMap class

	// print TreeSet using iterator
	public static void print(TreeSet set)
	{
		System.out.println("printing set ... ");
		Iterator iter = set.iterator();
		while (iter.hasNext())
			System.out.println(iter.next());
	}			// end of print(TreeSet) method

	// print TreeMap using get on each key in iterator
	public static void print(TreeMap map)
	{
		System.out.println("printing map ... ");
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext())
			System.out.println(map.get(iter.next()));
	}			// end of print(TreeMap) method
}		// end of TreeSetTreeMap method